QuickOPC User's Guide and Reference
Installed Examples - Web - UAWcfService1

A simple Web service using WCF technology. Provides a GetData method to read a value of an OPC UA variable. Use UAWcfClient1 project (under Console folder) to test this Web service.

The service class:

// $Header: $
// Copyright (c) CODE Consulting and Development, s.r.o., Plzen. All rights reserved.

// ReSharper disable ArrangeModifiersOrder

// UAWcfService1: A simple Web service using WCF technology. Provides a GetData method to read a value of an OPC UA 
// variable.
// Use UAWcfClient1 project (under Console folder) to test this Web service.

using OpcLabs.EasyOpc.UA;

namespace UAWcfService1
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        // Use a shared client instance to allow for better optimization.
        static private readonly EasyUAClient Client = new EasyUAClient();

        public string GetData()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // The following call may throw a UAException, which would be propagated to the client as a FaultException.
            // Production-quality code may need to catch the UAException and handle it differently.
            object value = Client.ReadValue(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845");
            return value?.ToString() ?? "";
        }
    }
}

 

Service contract:

// $Header: $
// Copyright (c) CODE Consulting and Development, s.r.o., Plzen. All rights reserved.

using System.ServiceModel;

namespace UAWcfService1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData();
    }
}

 

See Also

Installed Examples - Console

Conceptual